from flask import Blueprint, session, make_response
from io import BytesIO
# from cfg import redis_store
from ..utils.captcha import generate_image
tools_bp = Blueprint('tools', __name__, url_prefix='/api')


@tools_bp.route('/yzm')
def get_yam():
    yzm, code = generate_image(4, 4)
    buffer = BytesIO()
    yzm.save(buffer, 'JPEG')
    buf_bytes = buffer.getvalue()
    session['valid'] = code
    # redis_store.setnx('yzm_code', 60, code)
    response = make_response(buf_bytes)
    response.headers['Content-Type'] = 'image/jpg'
    return response